package StudentApply;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBUtil {
   private static Connection conn;
   static{
       try{
           //加载数据库
           String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
           String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=master;integratedSecurity=false;encrypt=true;trustServerCertificate=true;";
           String userName="sa";
           String userPwd="123456";
           //注册JDBC驱动并创建连接
           Class.forName(driverName);
           conn= DriverManager.getConnection(dbURL,userName,userPwd);
       }catch (Exception e){
           e.printStackTrace();
       }
   }
   //获取数据库连接
    public static Connection getConnection(){
       return conn;
    }
    //关闭数据库连接
    public static void closeconn(){
       try{
           if(conn!=null&&!conn.isClosed()) {
               conn.close();
           }
           }catch (Exception e){
           e.printStackTrace();
       }
    }
}
